import Vue from 'vue'

import Cookies from 'js-cookie'

import Element from 'element-ui'
import './assets/styles/element-variables.scss'

import '@/assets/styles/index.scss' // global css
import '@/assets/styles/ruoyi.scss' // ruoyi css
import App from './App'
import store from './store'
import router from './router'
import permission from './directive/permission'
import { download } from '@/utils/request'

import './assets/icons' // icon
import './permission' // permission control
import { getDicts } from "@/api/system/dict/data";
import { getConfigKey } from "@/api/system/config";
import { parseTime, resetForm, addDateRange, selectDictLabel, selectDictLabels, handleTree } from "@/utils/ruoyi";
import Pagination from "@/components/Pagination";
// 自定义表格工具扩展
import RightToolbar from "@/components/RightToolbar"
import i18n from './lang';

// 全局捕获未处理的Promise异常
Vue.config.errorHandler = (err, vm, info) => {
  if (err.isAxiosError) {
    console.warn('全局捕获的请求错误:', err.message)
    // 可在此添加统一错误处理逻辑
    return false // 阻止继续传播
  }
}

window.addEventListener('unhandledrejection', event => {
  if (event.reason && event.reason.isAxiosError) {
    event.preventDefault()
    console.error('未捕获的请求异常:', event.reason.message)
  }
})

// 全局方法挂载
Vue.prototype.getDicts = getDicts
Vue.prototype.getConfigKey = getConfigKey
Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.selectDictLabels = selectDictLabels
Vue.prototype.download = download
Vue.prototype.yjswShow=false//易加三维隐藏部分功能
Vue.prototype.handleTree = handleTree
Vue.prototype.tablePadding='5px 0'
Vue.prototype.msgSuccess = function (msg) {
  this.$message({ showClose: true, message: msg, type: "success" });
}

Vue.prototype.msgError = function (msg) {
  this.$message({ showClose: true, message: msg, type: "error" });
}

Vue.prototype.msgInfo = function (msg) {
  this.$message.info(msg);
}
Number.prototype.toFixed = function (Length, Rejection = 4) {
  const length = ~~Length; //取整
  const rejection = ~~Rejection; //取整
  if (length < 0 || length > 100) {
      throw new Error(
          "RangeError: toFixed() digits argument[0] must be between 0 and 100"
      );
  }
  if (rejection < 0 || rejection > 10) {
      throw new Error(
          "RangeError: toFixed() digits argument[1] must be between 0 and 10"
      );
  }
  let tStr = this.toString(),
      index = tStr.indexOf("."),
      arr = [tStr.slice(0, index)],
      res;
  if (index !== -1) {
      arr[1] = tStr.slice(index + 1, tStr.length);
  }
  if (this > Number.MAX_SAFE_INTEGER) {
      return Number(arr[0]).toExponential();
  }
  if (arr.length <= 1) {
      res = this.toString();
  } else {
      if (arr[1][length] > rejection) {
          // 截断到需要入的后一位
          arr[1] = arr[1].slice(0, length + 1);
          if (length === 0) {
              arr[0] = (Number(arr[0]) + 1).toString();
          } else {
              // 获取小数位前面有多少个0
              let zero = /^0+/.exec(arr[1]);
              zero = zero ? zero[0] : "";
              // 截断后加一
              arr[1] = arr[1].slice(0, length);
              // 记录原始长度
              const before = Number(arr[1]).toString();
              const later = (Number(arr[1]) + 1).toString();
              // 如果发生整体进位
              if (later.length > before.length || Number(arr[1]) === 0) {
                  if (zero.length === 0) {
                      // 整数位加1，小数位去掉最高位
                      arr[0] = Number(arr[0]) + 1;
                      arr[1] = later.slice(1, arr[1].length + 1);
                  } else {
                      // 将0字符的长度减小1
                      zero = zero.slice(0, zero.length - 1);
                      arr[1] = later;
                  }
              } else {
                  // 没有发生整体进位,直接计算小数位
                  arr[1] = later;
              }
              arr[1] = zero + arr[1];
          }
      } else {
          // 直接截断
          arr[1] = arr[1].slice(0, length);
      }
      res = length === 0 ? arr[0] : arr[0] + "." + arr[1];
  }
  // // 补全多余的0
  // if (arr[1] && arr[1].length < length) {
  //     for (let i = 0; i < length - arr[1].length; i++) res += "0";
  // } else if (!arr[1] && length !== 0) {
  //     res += ".";
  //     for (let i = 0; i < length; i++) res += "0";
  // }
  return res;
};
// 全局组件挂载
Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar)

Vue.use(permission)
Vue.use(Element, {
  i18n: (key, value) => i18n.t(key, value),
  size: Cookies.get('size') || 'medium' // set element-ui default size
})
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */

// Vue.use(Element, {

// })

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
})
